package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.RepBeneficioOtorgadoService;
import co.gov.medellin.ssti.sub.model.service.UsuarioSubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class SearchSelectionPermissionAction extends Action {
	@Autowired
	RepBeneficioOtorgadoService repBeneficioOtorgadoServiceImpl;
	@Autowired
	UsuarioSubprogramaService usuarioSubprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		// HttpSession session = request.getSession();

		String strCodeUser = "";
		// Collection<ProgramaVO> programas = null;
		Collection<SubprogramaVO> subprogramas = null;
		// List lUsuarios = new LinkedList();
		// List lConsultas = new LinkedList();
		// lUsuarios = (List) session.getAttribute("ListaUsuarios");
		List<UserVO> lGrupoUser = new LinkedList<UserVO>();

		Object ocoduser = request.getParameter("codeusuario");
		if (ocoduser != null) {
			strCodeUser = (String) ocoduser;
		}

		try {

			lGrupoUser = usuarioSubprogramaServiceImpl
					.findGrupoUsuarios(new Long(strCodeUser));

			if (lGrupoUser.size() > 0) {
				UserVO objUser = new UserVO();
				objUser = lGrupoUser.get(0);

				subprogramas = repBeneficioOtorgadoServiceImpl
						.findPermissionsReport(new Long(strCodeUser),
								objUser.getUserGroupName(), 0L,
								objUser.getCodeArea());
			}

		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}
		if (subprogramas.size() > 0) {
			request.setAttribute("ListaPermisos", subprogramas);
			request.setAttribute("ListaUsuario", lGrupoUser);
		} else
			request.setAttribute("msmerror",
					"El Usuario no tiene permisos para ver ningun beneficio");
		return mapping.findForward("success");
	}
}